//
//  ClaendarCollectionVC.m
//  CalendarAndLineLayout
//
//  Created by 骆驼鱼 on 16/11/10.
//  Copyright © 2016年 毒药_YX. All rights reserved.
//

#import "ClaendarCollectionVC.h"
#import "CalendarNilCell.h"
#import "CalendarCell.h"
#import "CalendarHeaderView.h"

@interface ClaendarCollectionVC ()
@end

static CGFloat const CellMargin = 1.0f;
static NSUInteger const DaysPerWeek = 7;
static NSUInteger year = 2016;
static NSUInteger month = 11;

@implementation ClaendarCollectionVC

static NSString * const reuseIdentifier = @"Cell";
#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
  
   [self.view reloadInputViews];
}

#pragma mark <UICollectionViewDataSource>

//返回section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  
  return 1;
}

//返回section中的cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  
  return [ClaendarTool getDaysOfMonth:year withMonth:month] + [ClaendarTool getWeekOfFirstDayOfMonth:year withMonth:month];
}

//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  
  CalendarNilCell * cell ;
  if (indexPath.row < [ClaendarTool getWeekOfFirstDayOfMonth:year withMonth:month]) {

 cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"CalendarNilCell" forIndexPath:indexPath];
    return cell;
  }else{
    CalendarCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CalendarCell" forIndexPath:indexPath];
    cell.dateLbl.text = [[NSString alloc] initWithFormat:@"%ld",indexPath.row - [ClaendarTool getWeekOfFirstDayOfMonth:year withMonth:month] + 1];
    return cell;
  }
}

#pragma mark <UICollectionViewDelegate>
//返回cell的宽和高
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  NSInteger numberOfMaigin = 9;
  CGFloat with = floorf((collectionView.frame.size.width - CellMargin * numberOfMaigin) / DaysPerWeek);
  CGFloat height = with;
  
  return CGSizeMake(with, height);
}

//每行cell之间的间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
  return CellMargin;
}

//返回头尾
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  if (kind == UICollectionElementKindSectionHeader) {
    CalendarHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CalendarHeaderView" forIndexPath:indexPath];
    headerView.headerTitle.text = [[NSString alloc] initWithFormat:@"%lu年%lu月",(unsigned long)year,(unsigned long)month];
    return headerView;
  }
  else
    return nil;
}

#pragma mark - private methods
- (IBAction)NextMonth:(UIButton *)sender {
  if (month < 12) {
    month += 1;
  }else{
    month = 1;
    year = year + 1;
  }
  
  [self.myCollectionView reloadData];

}
- (IBAction)lastMonth:(UIButton *)sender {
  if (month > 1) {
    month -= 1;
  }else{
    month = 12;
    year -= 1;
  }
  [self.myCollectionView reloadData];
}

@end
